//
//  BetterViewController.m
//  learn_async
//
//  Created by liboxiang on 2019/2/27.
//  Copyright © 2019 liboxiang. All rights reserved.
//

#import "BetterViewController.h"
#import "TableViewCell.h"
#import "CellModel.h"
#import "UILabel+Async.h"
const int LabelNumbet = 12;
@interface BetterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableview;

@property (strong, nonatomic) UILabel *testLabel;

@end

@implementation BetterViewController

//- (void)changeText {
//    NSMutableAttributedString *attributeStr = [self getAttributedStrWithStr:@"我是更改后的内容" iconName:[NSString stringWithFormat:@"%d",0] iconRect:CGRectMake(0, 0, 30, 30)];
//    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, attributeStr.length)];
//    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributeStr.length)];
//    _testLabel.asyncAttributedText = attributeStr;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _tableview = [UITableView new];
    _tableview.frame = self.view.bounds;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
    
    
//    _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 80, 50)];
//    _testLabel.backgroundColor = [UIColor blackColor];
//    NSMutableAttributedString *attributeStr = [self getAttributedStrWithStr:@"我在这里啊" iconName:[NSString stringWithFormat:@"%d",0] iconRect:CGRectMake(0, 0, 100, 30)];
//    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, attributeStr.length)];
//    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributeStr.length)];
//    _testLabel.asyncAttributedText = attributeStr;
//    [self.view addSubview:_testLabel];
//
//    UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
//    testButton.backgroundColor = [UIColor blackColor];
//    [testButton setTitle:@"重新赋值给Label" forState:UIControlStateNormal];
//    [testButton addTarget:self action:@selector(changeText) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:testButton];
    
//    __weak typeof(self) weakSelf = self;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [weakSelf changeText];
//    });
    
    
    
    NSString *str = @"阿迪克赖斯基饭卡的房间开了多少积分是肯德基疯狂拉升的妇科健康的失联飞机阿里斯顿就分开了撒娇的妇科了撒地方卡拉斯京的妇科辣椒水的风景阿里斯顿风景拉迪说妇科辣椒水淀粉及阿克苏地方就啊说的就分开了阿萨德放假啊说的就分开了多少就分开了说的妇科了就撒的咖啡冷静啊点开始就疯狂拉升江东父老看就撒的地方啊路上的风景看拉德斯基分开了倒计时风口浪尖阿斯顿飞机阿克索德减肥了时间的妇科啊收代理费加快了速度飞机喀拉峻啥地方了就阿萨德六块腹肌快乐圣诞节副书记地方啊丹江口市发动机开始妇科辣椒说的妇科了就啊的开始放假看的风景卡上的纠纷克拉的时间妇科辣椒水豆腐空间啊收代理费加快了速度风景卡上交电费；拉数据的妇科辣椒水都快疯了开始的风景啊就是的妇科啊说的妇科就撒开的风景卡的健身房卡迪说发动机开始妇科辣椒水的法拉第技术分";
    _dicArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < 30; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
        int iconHeight = arc4random()%25 + 5;
        for (int j = 0; j < LabelNumbet; j++) {
            CellModel *model = [CellModel  new];
            NSString *string = [str substringWithRange:NSMakeRange(arc4random() % 300, 20)];
            NSMutableAttributedString *attributeStr = [self getAttributedStrWithStr:string iconName:[NSString stringWithFormat:@"%d",j] iconRect:CGRectMake(0, 0, iconHeight, iconHeight)];
            model.font = arc4random() % 5 + 10;
            [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:model.font] range:NSMakeRange(0, attributeStr.length)];
            [attributeStr addAttribute:NSForegroundColorAttributeName value:(j % 2 == 0 ? [UIColor redColor] : [UIColor greenColor]) range:NSMakeRange(0, attributeStr.length)];
            model.str = attributeStr;
            
            [dic setObject:model forKey:[NSString stringWithFormat:@"%d",j]];
        }
        [dic setObject:[NSString stringWithFormat:@"%.2f",[TableViewCell cellHeigtForDic:dic]] forKey:@"height"];
        [_dicArray addObject:dic];
    }
    [TableViewCell registerForTable:self.tableview];
}

- (NSMutableAttributedString *)getAttributedStrWithStr:(NSString *)str iconName:(NSString *)iconName iconRect:(CGRect)iconRect
{
    NSMutableAttributedString *attri = [NSMutableAttributedString new];
    //NSTextAttachment可以将要插入的图片作为特殊字符处理
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    //定义图片内容及位置和大小
    attch.image = [UIImage imageNamed:iconName];
    attch.bounds = iconRect;
    //创建带有图片的富文本
    NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attch];
    [attri appendAttributedString:imageStr];
    NSAttributedString *strAttri = [[NSAttributedString alloc] initWithString:str];
    [attri appendAttributedString:strAttri];
    return attri;
}

#pragma mark - UITableViewDelegate\UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[_dicArray[indexPath.row] objectForKey:@"height"] floatValue];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dicArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[TableViewCell identifier] forIndexPath:indexPath];
    [cell asyncRefreshWithDic:_dicArray[indexPath.row]];
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
